package app.controllers.admin.game;

import app.Const;
import app.models.DaoKit;
import app.models.game.GameCategory;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * 游戏类型
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-02-12 13:41
 * @since JDK 1.6
 */
public class CategoryController extends BasicController {

    public void index() {
        render("index.ftl");
    }

    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        Page<GameCategory> page = GameCategory.dao.datatable(criterias);

        renderDataTables(page, criterias);
    }

    public void create() {
        GameCategory category = new GameCategory();
        setAttr("oper", "create");
        setAttr("category", category);
        render("item.ftl");
    }

    public void edit() {
        GameCategory category = GameCategory.dao.findById(getPara());

        setAttr("oper", "edit");
        setAttr("category", category);
        render("item.ftl");
    }

    public void save() {
        GameCategory category = getModel(GameCategory.class, "category");
        if (DaoKit.isNew(category)) {
            category.save();
        } else {
            category.update();
        }
        redirect("/admin/game/category/index");
    }

    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("gamecategory.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }

}
